package com.aaa.service.impl;

import com.aaa.common.domain.Dept;
import com.aaa.common.query.DeptQuery;
import com.aaa.common.result.Result;
import com.aaa.mapper.DeptMapper;
import com.aaa.service.DeptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private DeptMapper mapper;



    /**
     * 分页查询部门表
     * @param deptQuery
     * @return
     */
    @Override
    public Result listDept(DeptQuery deptQuery) {
        Page<Dept> page = new Page<>(deptQuery.getPage(),deptQuery.getCount());
        Page<Dept> deptPage = mapper.DeptListP(page, deptQuery.getName(),deptQuery.getMinTime(),deptQuery.getMaxTime());
        return Result.success(deptPage);
    }

    /**
     * 部门表添加
     * @param dept
     * @return
     */
    @Override
    public Result addDept(Dept dept) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        List<Dept> depts = mapper.selectList(wrapper);
        for (Dept dept1 : depts) {
            if (dept1.getName().equals(dept.getName())){
                return Result.error("部门名称已存在");
            }
        }
        int insert = mapper.insert(dept);
        if (insert>0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 部门表修改
     * @param dept
     * @return
     */
    @Override
    public boolean updateDept(Dept dept) {
        int updateById = mapper.updateById(dept);
        if (updateById>0){
            return true;
        }
        return false;
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @Override
    public Result listUserById(Integer id) {
        Dept dept = mapper.queryById(id);
        return Result.success(dept);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Result delByIds(String ids) {
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        int delete = mapper.delete(strings);
        if (delete>0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 部门表删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteDept(String ids) {
        boolean delete = mapper.updateDept(ids);
        if (delete==true){
            return true;
        }
        return false;
    }
}
